package cn.com.pet.common.base;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "serial" })
public class BaseAction extends ActionSupport implements ServletRequestAware,
		SessionAware, ServletResponseAware {

	public HttpServletRequest request;

	public HttpServletResponse response;

	public Map<String, Object> session;

	public String basePath;

	public int pageSize = 10;

	public int page = 1;

	public int totalPage;

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getBasePath() {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path;
		if (!this.session.containsKey("basePath")) {
			this.session.put("basePath", basePath);
		}

		return basePath;
	}

	public void setBasePath(String path) {
		this.basePath = path;
	}

	public void prepare() throws Exception {
		this.setBasePath(this.getRequest().getContextPath());
	}

}
